﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AgencyEntityLib.BaseEntity;
using FormControllers;
using Ecopsy.Tools;
using SqlLibrary;

public partial class iFrameAdmin_Discounts : iAdmin_Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) MyDataBind(); 

    }

    public Discount[] GetList(int startRowIndex, int maximumRows, out int RowCount)
    {

        wsAgency _ws = new wsAgency();
        ListWhere w = (ListWhere)Where;
        w.ListLoadStatment.StartRowIndex = startRowIndex;
        w.ListLoadStatment.EndRowIndex = startRowIndex + maximumRows;
        DiscountList l = _ws.GetSurfaceGroupList((DiscountList.sqlLoadStatment)w.ListLoadStatment);
        RowCount = l.OutputRwCnt;

        //заряжаем comparer функцией сортировки 
        Where.SortCondition.fnOrderBy = SortFunctions.fOrderBy;
        // Сортируем
        Discount[] r = l.ToArray<Discount>();
        Array.Sort(r, Where.SortCondition);
        return r;
    }

    protected void Find_Click(object sender, ImageClickEventArgs e)
    {
        string sFlt = txtSearch.Text;
       // int errPosition = -1;
        //if (!SqlLibrary.ExpressionChecker.CheckStatment(ref sFlt, ref errPosition))
        //{
        //    ScriptManager.RegisterStartupScript(this, GetType(), "AlertBadFiltr", "alert('Ошибочное выражение фильтрации');", true);
        //    return;
        //}

        ((DiscountList.sqlLoadStatment)Where.ListLoadStatment).Lexeme = sFlt;
        MyDataBind();
    }

    protected void AddButton_Click(object sender, EventArgs e)
    {
        string s = "iFrameAdmin_EditDiscount.aspx?" + qDiscountID + "=" + GlobalConstants.qPrefNEW_RECORD + Guid.NewGuid().ToString();
        Response.Redirect(s);
    }

    private void MyDataBind()
    {
        ObjectDS_PF ODS = new ObjectDS_PF(GetList);
        lvDiscounts.DataSource = ODS.ImAsObjectDataSource;
        DataBind();

    }

    protected void OnDeleteClick(object sender, EventArgs e)
    {
        ImageButton btn = (ImageButton)sender;
        GlobalConstants.CurrentAgency.SurfaceGroups.Remove(btn.CommandArgument);

        wsAgency ws = new wsAgency();
        ws.SaveAgency();
        MyDataBind();

    }

    protected override ListWhere Where
    {
        get
        {
            return f_Where(new DiscountList.sqlLoadStatment(), "Name");
        }
    }

    protected void lvSort(object sender, EventArgs e)
    {
        
        f_lvSort(sender, lvDiscounts , MyDataBind);
    }
    
    protected string GetEditCommand(string ID)
    {
        //string s = "iFrameAdmin_EditDiscount.aspx?" + qDiscountID + "=" + ID + "";
        //return "alert('zxzxzx'); document.location ="+s ;
        return "Redirect('" + ID + "');";
    }

    protected void EditDiscount(object sender, EventArgs e)
    {
        LinkButton btn = (LinkButton)sender;
        bool isEnabled = true;
        string sUrlKey = GlobalConstants.SiteMapItem_iFrameAdmin_EditDiscount ;
        iPageController.SetPageUrl(ref sUrlKey, ref isEnabled);
        string s = "PageProcessor.aspx?Page=" + sUrlKey + ".aspx?" + qDiscountID + "=" + btn.CommandArgument + "";
        Response.Redirect(s);
    }

    protected void lvSurfaceGroups_PagePropertiesChanged(object sender, EventArgs e)
    {
       MyDataBind();
    }
}
